<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="产品名称">
                <a-input v-model="queryParam.productName" placeholder="请输入" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="产品编号">
                <a-input v-model="queryParam.productCode" placeholder="请输入" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="修改时间">
                <a-range-picker
                  v-model="updateDateRange"
                  valueFormat="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  allow-clear
                />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="产品材料">
                <a-input v-model="queryParam.material" placeholder="请输入" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="产品类型">
                <a-select
                  v-model="queryParam.productType"
                  placeholder="请选择类型"
                  style="width: 100%"
                  allow-clear
                  optionFilterProp="label"
                >
                  <a-select-option
                    :label="p.dictLabel"
                    v-for="(p, index) in productTypes"
                    :key="index"
                    :value="p.dictValue"
                  >{{ p.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :md="24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="{ float: 'right', overflow: 'hidden' }"
              >
                <a-button type="primary" @click="handleQuery">
                  <a-icon type="search" />查询
                </a-button>
                <a-button style="margin-left: 8px" @click="resetQuery">
                  <a-icon type="redo" />重置
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="$refs.createForm.handleAdd()">
          <a-icon type="plus" />新增
        </a-button>
        <a-button
          type="primary"
          :disabled="single"
          @click="$refs.createForm.handleUpdate(undefined, ids)"
        >
          <a-icon type="edit" />修改
        </a-button>
        <a-button type="danger" :disabled="multiple" @click="handleDelete">
          <a-icon type="delete" />删除
        </a-button>
        <a-button type="dashed" @click="$refs.importExcel.importExcelHandleOpen()">
          <a-icon type="import" />导入
        </a-button>
        <a-button type="primary" @click="handleExport">
          <a-icon type="download" />导出
        </a-button>
        <a-button
          type="dashed"
          shape="circle"
          :loading="loading"
          :style="{ float: 'right' }"
          icon="reload"
          @click="getList"
        />
      </div>

      <!-- 增加修改 -->
      <create-form ref="createForm" @ok="getList" />
      <product-details ref="productDetails" />
      <!-- 上传文件 -->
      <import-excel ref="importExcel" @ok="getList" />

      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        :size="tableSize"
        rowKey="productId"
        :columns="columns"
        :data-source="list"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false"
        @change="changeTab"
      >
        <span slot="updateTime" slot-scope="text, record">{{ parseTime(record.updateTime) }}</span>
        <span slot="productType" slot-scope="text, record">{{ getProductType(record.productType) }}</span>
        <span slot="operation" slot-scope="text, record">
          <a @click="$refs.createForm.handleUpdate(record, undefined)">
            <a-icon type="edit" />修改
          </a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record)">
            <a-icon type="delete" />删除
          </a>
          <a-divider type="vertical" />
          <a @click="$refs.productDetails.openDetails(record.productId)">
            <a-icon type="bars" />详情
          </a>
        </span>
      </a-table>

      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import CreateForm from './modules/CreateForm'
import ProductDetails from './modules/ProductDetails'
import { listProduct, delProduct } from '@/api/erp/product'
import ImportExcel from './modules/ImportExcel'

export default {
  name: 'Storage',
  components: {
    CreateForm,
    ImportExcel,
    ProductDetails
  },
  data () {
    return {
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        productCode: undefined,
        productType: undefined,
        productStatus: undefined,
        material: undefined,
        params: {}
      },
      // 日期范围
      updateDateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      loading: false,
      columns: [
        {
          title: '产品编号',
          dataIndex: 'productCode',
          width: 100,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          width: 100,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '产品类型',
          dataIndex: 'productType',
          width: 70,
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'productType' }
        },
        {
          title: '产品数量',
          dataIndex: 'finished',
          width: 50,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '产品材料',
          dataIndex: 'material',
          width: 100,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
          width: 120,
          ellipsis: true,
          align: 'center',
          sorter: true,
          scopedSlots: { customRender: 'updateTime' },
          sortDirections: ['descend', 'ascend']
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 120,
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ],
      list: [],
      total: 0,
      selectedRowKeys: [],
      selectedRows: [],
      productTypes: [],
      ids: [],
      names: [],
      sort: undefined,
      field: undefined
    }
  },
  created () {
    this.getList()
    this.getDicts('xzg_product_type').then(response => {
      this.productTypes = response.data
    })
  },
  methods: {
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        productName: undefined,
        productCode: undefined,
        productType: undefined,
        productStatus: undefined,
        material: undefined,
        params: {}
      }
      this.updateDateRange = []
      this.sort = undefined
      this.field = undefined
      this.handleQuery()
    },
    getList () {
      this.loading = true
      const param = this.addDateRange(this.queryParam, this.updateDateRange, 'update')
      if (this.sort !== undefined && this.field !== undefined) {
        param.params['sort'] = this.sort
        param.params['field'] = this.field
      }
      listProduct(param).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const productIds = row.productId || this.ids
      const productNames = row.productName || this.names
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中产品为' + productNames + '的数据',
        onOk () {
          return delProduct(productIds).then(() => {
            that.onSelectChange([], [])
            that.getList()
            that.$message.success(
              '删除成功',
              3
            )
          })
        }
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('erp/product/export', {
            ...that.queryParam
          }, `product_${new Date().getTime()}.xlsx`)
        },
        onCancel () { }
      })
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.productId)
      this.names = this.selectedRows.map(item => item.productName)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    getProductType (type) {
      for (var i = 0; i < this.productTypes.length; i++) {
        if (this.productTypes[i].dictValue === type) {
          return this.productTypes[i].dictLabel
        }
      }
    },
    changeTab (p, f, { field, order }) {
      this.sort = order === 'descend' ? 'desc' : 'asc'
      this.field = field
      this.getList()
    }
  }
}
</script>
